package net.root.plain.main;

import java.util.ArrayList;

import net.root.plain.alarm.AlarmDAO;
import net.root.plain.alarm.AlarmDTO;
import net.root.plain.alarm.AlarmEditAdapter;
import net.root.plain.alarm.WeekUtil;
import net.root.plain.util.DBConn;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

public class AlarmEditActivity extends Activity {
	private AlarmEditAdapter adapter;
	private ListView listView;
	private AlarmDTO dto;
	private AlarmDAO dao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_edit);
		Intent intent = getIntent();
		dto = new AlarmDTO();
		int no = 0;
		dao = new AlarmDAO(DBConn.getConnection(this));
		try {
			no = intent.getExtras().getInt(DBConn.columns[0]);
			dto = dao.getReadData(no);
		} catch (NullPointerException e) {
		}
		if (no == 0) {
			dto.setNo(dao.getMaxNo() + 1);
			dto.setAlarmBell("Bell");
			dto.setAlarmLabel("Label");
			dto.setAlarmRepeat(1);
			dto.setAlarmStatus(0);
			dto.setAlarmTime(String.valueOf(System.currentTimeMillis()));
			dto.setAlarmVibrate(0);
			dao.insertData(dto);
		}
		DBConn.close();
		
		ArrayList<String> list = new ArrayList<String>();
		list.add(String.valueOf(dto.getNo()));
		list.add(dto.getAlarmBell());
		list.add(dto.getAlarmLabel());
		list.add(WeekUtil.week(dto.getAlarmRepeat()));
		list.add(dto.getAlarmStatus()==1?"true":"false");
		list.add(dto.getAlarmTime());
		list.add(dto.getAlarmVibrate()==1?"true":"false");
		adapter = new AlarmEditAdapter(this, dto);
		listView = (ListView)findViewById(R.id.alarm_edit_list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(adapter);
//		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
//				android.R.layout.simple_list_item_1, list);
//		
//		listView = (ListView)findViewById(R.id.alarm_edit_list);
//		listView.setAdapter(adapter1);
	}
}
